package persistencia.HSQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import modelo.Curso;
import persistencia.DAOS.CursoDAO;

public class HSQLCursoDAO implements CursoDAO {
	private static Connection connection;
	
	public static void conectar(){
		connection = null;
		try {
			Class.forName("org.hsqldb.jdbcDriver");
			connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "sa", ""); 	
		}
		
		catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public static void desconectar(){
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public ArrayList<Curso> cargarCurso(Integer in) {
		ArrayList<Curso> lista = null;
		conectar();	
		
		try {
			Statement stm = connection.createStatement();
			ResultSet rs = stm.executeQuery("SELECT DISTINCT cu.idCurso, cu.nombre FROM Curso cu, Carrera ca WHERE cu.idCarrera = " + in);
			
			while(rs.next()){
				if(lista == null)
					lista = new ArrayList<Curso>();
				
				lista.add(new Curso(rs.getInt("idCurso"), rs.getString("nombre")));
			}
			
			desconectar();
			stm.close();
		}
		
		catch (SQLException e) {
			e.printStackTrace();
		}
	
		return lista;
	}
}
